﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CallCenter.Data;
using CallCenter.Data.BizUtilities;
using System.Configuration;
using System.Data;
using CallCenter.Security;
using CallCenter.Data.Models;
using CallCenter.Data.Misc;

namespace CallCenter.Web.Controllers
{
    [AuthorizeCore]
    public class CompanyOfficeController : BaseController
    {
        // GET: CompanyOffice
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CompanyOfficeHistory()
        {
            CompanyOfficeViewModel cmpy = new CompanyOfficeViewModel();
            return View(cmpy);
        }

        [HttpPost]
        public JsonResult GetCompanyOffice(CompanyOfficeViewModel model,int id)
        {
            model.companyId = id;
            var scratchPads = this.Bridge.CompanyOfficeUtility.GetAllGrid(model.companyId);
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

     
        public JsonResult GetAllCompanyOffice()
        {
            var scratchPads = this.Bridge.CompanyOfficeUtility.GetAllCompanyOffices();
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetCompanyOfficeByCompanyId(int id)
        {
            var scratchPads = this.Bridge.CompanyOfficeUtility.GetCompanyOfficesByCompanyId(id);
            if (scratchPads.Count <= 0)
            {
                List<CompanyOfficeViewModel> companyOfficeModelList = new List<CompanyOfficeViewModel>();
                CompanyOfficeViewModel companyOfficeModel = new CompanyOfficeViewModel();
                companyOfficeModel.officeId = 0;
                companyOfficeModel.companyoffice = "None";
                companyOfficeModelList.Add(companyOfficeModel);
                scratchPads = companyOfficeModelList;
            }
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }



        [HttpPost]
        public JsonResult CreateCompanyOffice(CompanyOfficeViewModel model)
        {
           
           var returnMessage = this.Bridge.CompanyOfficeUtility.InsertToCompany(model);

            if (!returnMessage.Status)
                ModelState.AddModelError("Error", returnMessage.Message);
            else {
                return Json(new AjaxSuccess { Message = "Success" }, JsonRequestBehavior.AllowGet);
            }
          

            return Json(new AjaxFailure { Errors = ModelState.SerializeErrors }, JsonRequestBehavior.DenyGet);
        }


        [HttpPost]
        public JsonResult UpdateCompanyOffice(CompanyOfficeViewModel model)
        {
           

            var returnMessage = this.Bridge.CompanyOfficeUtility.UpdateToCompany(model);
            if (!returnMessage.Status)
                ModelState.AddModelError("Error", returnMessage.Message);
            else {
                return Json(new AjaxSuccess { Message = "Success" }, JsonRequestBehavior.AllowGet);
            }
           

            return Json(new AjaxFailure { Errors = ModelState.SerializeErrors }, JsonRequestBehavior.DenyGet);
        }


        [HttpPost]
        public JsonResult DeleteCompanyOffice(CompanyOfficeViewModel model)
        {
           
            int id = model.id;
            this.Bridge.CompanyOfficeUtility.Delete(id);
            var returnMessage = "Success";
            return Json(returnMessage, JsonRequestBehavior.AllowGet);

        }

    }
}